from cqssc.lottery import Lottery
from cqssc.db.mysql import Mysql
from cqssc.buy.forecast_big import ForecastBig
import pymysql


class BigOrSmall:
    lottery_period = None
    lottery_info = None
    current_period = None
    current_numbers = None
    db = None
    location = 1
    table = "big_or_small"
    cardinality_number = 10
    multiple_number = 1
    number = 0
    data = {'period': '',
            'number': '0',
            "location": "1",
            "location_number": "0",
            "is_big": 1,
            "pass": 0,
            "multiple": 1,
            "cardinality": 10,
            "win": 0,
            "money": 0}

    # lottery,format: { "period":"170604-001","number":"12345" }
    def __init__(self, lottery, location=1,table=None):
        print(location)
        # self.lottery_period = Lottery()
        self.lottery_info = lottery
        self.current_period = lottery["period"]
        self.current_numbers = lottery['number']
        self.db = Mysql()
        self.location = int(location)
        self.number = int(self.current_numbers[(self.location-1)])

        if table is not None:
            self.table = table

    # 查找本期数据
    def findCurrentData(self):
        find_data_sql = r"select * from `{table}` where period=%s and location=%s".format(table=self.table)
        self.db.find_cursor = self.db.connection.cursor(pymysql.cursors.DictCursor)
        self.db.find_cursor.execute(find_data_sql, (self.current_period, self.location))
        result = self.db.find_cursor.fetchone()

        if result is None:
            return False
        else:
            return result

    # 开奖标记,是否中奖
    def lottery(self):
        result = self.findCurrentData()
        # 如果没有开奖数据，则将本次开奖记录默认为中奖状态插入购买表
        # 但是购买金额为 0
        if result is False:
            self.newData()
        else:
            id = result["id"]
            is_big = int(result['is_big'])
            number_big = self.is_big()
            is_win = 0

            if is_big == number_big:
                # @tudo: 统计今天胜局
                is_win = 1

            updated_is_win_sql = r"update `{table}` set `win` = %s,`number`=%s,`location_number`=%s where id = {id}".format(
                table=self.table, id=id)
            sta = self.db.cursor.execute(updated_is_win_sql, (is_win, self.current_numbers, self.number))

        #开奖完成后，开始计算下一期（目前未开奖的）的购买记录
        self.next_lottery()


    #将本次开奖作为新数据插入购买记录
    def newData(self):
        self.data["period"] = self.current_period
        self.data["number"] = self.current_numbers
        self.data["location"] = self.location
        self.data["location_number"] = self.number
        self.data["is_big"] = self.is_big()
        self.data["win"] = 1
        # print(self.data)
        sql_values = tuple(self.data.values())
        new_data_sql = r"insert into `{table}`(`period`,`number`,`location`,`location_number`,`is_big`,`pass`,`multiple`,`cardinality`," \
                       r"`win`,`money`) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".format(table=self.table)
        sta = self.db.cursor.execute(new_data_sql, sql_values)
        self.db.connection.commit()
        return sta

    # 大小数判断
    def is_big(self):
        if self.number < 5:
            return 0
        else:
            return 1

    # 下次开奖预测，插入数据，标记下一期开奖（当期未开奖）
    def next_lottery(self):
        forecast = ForecastBig(period=self.current_period,location=self.location)
        forecast.forecast()

    # 计算倍数
    def multiple(self):
        return self.multiple_number

    # 获取基数
    def cardinality(self):
        return self.cardinality_number


if __name__ == "__main__":
    test = BigOrSmall(lottery={"period": "170604-002", "number": "12345"}, location=2)
    print(test.lottery())
